#include <napi.h>
#include <ApplicationServices/ApplicationServices.h>

void SimulateKeyPress(CGKeyCode keyCode) {
    CGEventRef keyDownEvent = CGEventCreateKeyboardEvent(NULL, keyCode, true);
    CGEventRef keyUpEvent = CGEventCreateKeyboardEvent(NULL, keyCode, false);

    CGEventPost(kCGHIDEventTap, keyDownEvent);
    CGEventPost(kCGHIDEventTap, keyUpEvent);

    CFRelease(keyDownEvent);
    CFRelease(keyUpEvent);
}

Napi::Value SimulateCtrlC(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    SimulateKeyPress(kCGEventFlagMaskControl);
    SimulateKeyPress(kVK_ANSI_C);

    return env.Undefined();
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
    exports.Set("simulateCtrlC", Napi::Function::New(env, SimulateCtrlC));
    return exports;
}

NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)